/**
 * 
 */
package com.yidu.springbootdemo.permission.service;

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.model.AtsEmpAthlete;
import com.yidu.hbats.model.AtsEmpCoach;
import com.yidu.hbats.model.AtsEmpExpert;
import com.yidu.hbats.model.AtsEmpLeader;
import com.yidu.hbats.service.emp.AtsEmpAthleteService;
import com.yidu.hbats.service.emp.AtsEmpCoachService;
import com.yidu.hbats.service.emp.AtsEmpExpertService;
import com.yidu.hbats.service.emp.AtsEmpLeaderService;
import com.yidu.springbootdemo.permission.action.vo.TuserVo;
import com.yidu.springbootdemo.permission.dao.TroleUserMapper;
import com.yidu.springbootdemo.permission.dao.TuserMapper;
import com.yidu.springbootdemo.permission.domain.TroleUser;
import com.yidu.springbootdemo.permission.domain.Tuser;
import com.yidu.springbootdemo.permission.tools.Pages;
import com.yidu.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 *	类的描述：	用户service接口实现类
 * @author HJJ
 * @date 2019年9月25日 下午3:25:11
 */
@Service
public class TuserServiceImp implements TuserService {

	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource
	private TuserMapper tuserMapper;
	
	@Resource
	private TroleUserMapper troleUserMapper;

	@Resource
	TroleUserService troleUserService;

	/**
	 * 中心领导信息接口
	 */
	@Resource
	private AtsEmpLeaderService atsEmpLeaderService;

	/**
	 * 教练员信息接口
	 */
	@Resource
	private AtsEmpCoachService atsEmpCoachService;

	/**
	 * 专家信息接口
	 */
	@Resource
	private AtsEmpExpertService atsEmpExpertService;

	/**
	 * 运动员信息接口
	 */
	@Resource
	private AtsEmpAthleteService atsEmpAthleteService;
	
	@Override
	public void add(Tuser tch) {
		tuserMapper.insertSelective(tch);
		
		TroleUser ud1 = new TroleUser();
		if(tch.getJuese() == null) {
			String rd1 = UUID.randomUUID().toString().replaceAll("-", "");
			ud1.setRuId(rd1);
			ud1.setChaId(null);
			ud1.setUserId(tch.getUserId());
			troleUserMapper.insertSelective(ud1);
		}else {
			for (int i = 0; i < tch.getJuese().length; i++) {
				TroleUser ud = new TroleUser();
				String rd = UUID.randomUUID().toString().replaceAll("-", "");
				ud.setRuId(rd);
				//ud.setTch(tuserMapper.findByID2(tch.getJuese()[i]));
				//ud.setTus(tch);
				ud.setChaId(tch.getJuese()[i]);
				ud.setUserId(tch.getUserId());
				//set.add(ud);
				troleUserMapper.insertSelective(ud);
			}
		}

		if (!StringUtils.isEmpty(tch.getFilId()) && !StringUtils.isEmpty(tch.getCompanyNum())) {
			if (tch.getCompanyNum().equals("1")) {//领导
				//根据领导id修改remark
				AtsEmpLeader atsEmpLeader = new AtsEmpLeader();
				atsEmpLeader.setLeaderId(Integer.valueOf(tch.getFilId()));
				atsEmpLeader.setRemark("N");
				atsEmpLeaderService.saveOrUpdate(atsEmpLeader);
			} else if (tch.getCompanyNum().equals("2")) {//教练员
				//教练员
				AtsEmpCoach atsEmpCoach = new AtsEmpCoach();
				atsEmpCoach.setCoachId(Integer.valueOf(tch.getFilId()));
				atsEmpCoach.setRemark("N");
				atsEmpCoachService.saveOrUpdate(atsEmpCoach);
			} else if (tch.getCompanyNum().equals("3")) {//专家
				//专家
				AtsEmpExpert atsEmpExpert = new AtsEmpExpert();
				atsEmpExpert.setExpertId(Integer.valueOf(tch.getFilId()));
				atsEmpExpert.setRemark("N");
				atsEmpExpertService.saveOrUpdate(atsEmpExpert);
			} else if (tch.getCompanyNum().equals("4")) {//运动员
				//运动员 修改状态
				AtsEmpAthlete atsEmpAthlete = new AtsEmpAthlete();
				atsEmpAthlete.setAthleteId(Integer.valueOf(tch.getFilId()));
				atsEmpAthlete.setRemark("N");
				atsEmpAthleteService.saveOrUpdate(atsEmpAthlete);
			}
		}
	}
	@Override
	public void delete(String modID) {

		//根据id先查
		TuserVo tu = findByID(modID);
		if (!StringUtils.isEmpty(tu.getFilId())) {
			if (tu.getCompanyNum().equals("1")) {//领导
				//根据领导id修改remark
				AtsEmpLeader atsEmpLeader = new AtsEmpLeader();
				atsEmpLeader.setLeaderId(Integer.valueOf(tu.getFilId()));
				atsEmpLeader.setRemark("Y");
				atsEmpLeaderService.saveOrUpdate(atsEmpLeader);
			} else if (tu.getCompanyNum().equals("2")) {//教练员
				//教练员
				AtsEmpCoach atsEmpCoach = new AtsEmpCoach();
				atsEmpCoach.setCoachId(Integer.valueOf(tu.getFilId()));
				atsEmpCoach.setRemark("Y");
				atsEmpCoachService.saveOrUpdate(atsEmpCoach);
			} else if (tu.getCompanyNum().equals("3")) {//专家
				//专家
				AtsEmpExpert atsEmpExpert = new AtsEmpExpert();
				atsEmpExpert.setExpertId(Integer.valueOf(tu.getFilId()));
				atsEmpExpert.setRemark("Y");
				atsEmpExpertService.saveOrUpdate(atsEmpExpert);
			} else if (tu.getCompanyNum().equals("4")) {//运动员
				//运动员 修改状态
				AtsEmpAthlete atsEmpAthlete = new AtsEmpAthlete();
				atsEmpAthlete.setAthleteId(Integer.valueOf(tu.getFilId()));
				atsEmpAthlete.setRemark("Y");
				atsEmpAthleteService.saveOrUpdate(atsEmpAthlete);
			}
		}
		// 先删关系再删用户
		troleUserService.delete(modID);
		//删除用户
		tuserMapper.deleteByPrimaryKey(modID);
	}

	@Override
	public void update(Tuser tch) {
		tuserMapper.updateByPrimaryKeySelective(tch);
	}

	@Override
	public List<TuserVo> findAll(TuserVo tch,Pages pages1) {
		PageHelper.offsetPage(pages1.getStartRows(), pages1.getLimit());
		Tuser po = new Tuser();
		BeanUtils.copyProperties(tch, po);
		List<TuserVo> list2 = new ArrayList<TuserVo>();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("po", po);
		map.put("pages", pages1);
		List<Tuser> list = tuserMapper.findAll(map);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Tuser tuser = (Tuser) iterator.next();
			TuserVo tch2 = new TuserVo();
			BeanUtils.copyProperties(tuser, tch2);
			list2.add(tch2);
		}
		PageHelper.startPage(pages1.getStartRows(), pages1.getLimit());
		return list2;
	}

	@Override
	public TuserVo findByID(String chaID) {
		Tuser u = tuserMapper.findByID(chaID);
		TuserVo vo = new TuserVo();
		BeanUtils.copyProperties(u, vo);
		return vo;
	}

	@Override
	public List<Tuser> findUser(Tuser us,String time) {
		//return tuserMapper.findUser(us,time);
		return null;
	}

	@Override
	public int count(TuserVo mod) {
		Tuser us = new Tuser();
		BeanUtils.copyProperties(mod, us);
		return tuserMapper.count(us);
	}

	@Override
	public List<Tuser> login(Tuser cond) {
		
		return tuserMapper.login(cond);
	}
	/**
	 * 使用redis保存缓存数据,当角色模块发生改变时,删除所有的缓存
	 */
	@Resource
	RedisUtil redisUtil ;

	@Override
	public Tuser findUser2(String name, String password) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userName",name);
		map.put("userPassword",password);
		Tuser us = tuserMapper.findUser(map);
		if (us != null) {
			Tuser user2 = (Tuser) redisUtil.get("oursUser_" + us.getUserName());
			logger.info("使用redis缓存......" + user2);
			logger.info("oursUser_" + us.getUserName());
			if (user2 != null) {
				logger.info("使用redis缓存......");
				return user2;
			}
			redisUtil.set("oursUser_"+us.getUserName(),us);
		}
		return us;
	}

	@Override
	public Tuser findBycompanyNum(Tuser tuser) {
		return tuserMapper.findBycompanyNum(tuser);
	}

    @Override
    public List<Tuser> selBycompanyNum(String companyNum) {
        return tuserMapper.selBycompanyNum(companyNum);
    }

	@Override
	public List<TuserVo> selAll() {
		 List<Tuser> tusers = tuserMapper.selAll();
		 List<TuserVo> tuserVos = new ArrayList<TuserVo>();
		for (Tuser tuser : tusers) {
			TuserVo vo = new TuserVo();
			BeanUtils.copyProperties(tuser, vo);
			tuserVos.add(vo);
		}
		 return tuserVos;
	}

}
